package com.dajie.pt.service.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dajie.sms.SmsServicesContext;
import com.dajie.sms.service.SmsService;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by yunbinan on 15-6-11.
 */
public class SendSms {
    private static SmsService smsService = SmsServicesContext.getInstance().getSmsService();
    private static Logger logger = LoggerFactory.getLogger(SendSms.class);
    private JSONArray dataList = new JSONArray();

    public static String sendSms(int type, int tpl, Map map) {
        String result = smsService.sendSMS(type, tpl, map).getResult();
        return result;
    }

    public SendSms() {

    }

    public String sendSmsBatchNew(String mobile, String tplId, Map<String, String> map) {
        String resData = null;
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost method = new HttpPost("http://sms.nuggets.dajie.com/sendSms?sendType=1");
        // 接收参数json列表
        try {
            JSONObject jsonParam = new JSONObject();
            JSONObject param = new JSONObject();
            JSONObject datas = new JSONObject();
            for (String key : map.keySet()) {
                param.put(key, map.get(key));
            }
            jsonParam.put("tpl_id", tplId);
            jsonParam.put("mobile", mobile);
            jsonParam.put("param", param);
            dataList.add(jsonParam);

            if (dataList.size() >= 200) {
                datas.put("data", dataList);
                //    logger.info("SendSms -->dataSize:{} dataJsonStr:{}", dataList.size(), datas.toString());
                logger.info("SendSms --> dataSize:{}", dataList.size());
                StringEntity entity = new StringEntity(datas.toString(), "utf-8");//解决中文乱码问题
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                method.setEntity(entity);
                HttpResponse result = httpClient.execute(method);
                resData = EntityUtils.toString(result.getEntity());
                resData = resData.substring(1, resData.length() - 1);
                // resData = "5";
                logger.info("SendSms --> result:{}", resData);
                if (resData != null) {
                    for (int i = 0; i < dataList.size(); i++) {
                        logger.info("SendSms --> sendSms success  jasonArray{} ", dataList.get(i).toString());
                    }
                }
                dataList.clear();
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("SendSms --> sendSmsBatchNew ERROR:{}" + e.getMessage(), e);
        }
        return resData;
    }

    public JSONArray getLeftSms() {
        return dataList;
    }

    public String sendSmsBatch(JSONArray dataLists) {
        String resData = null;
        logger.info("SendSms --> left num:" + dataLists.size());

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost method = new HttpPost("http://sms.nuggets.dajie.com/sendSms?sendType=1");
        // 接收参数json列表
        try {
            JSONObject datas = new JSONObject();
            datas.put("data", dataLists);
            //  logger.info("SendSms --> dataJsonStr:" + datas.toString());
            StringEntity entity = new StringEntity(datas.toString(), "utf-8");//解决中文乱码问题
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            method.setEntity(entity);
            HttpResponse result = httpClient.execute(method);
            resData = EntityUtils.toString(result.getEntity());
            resData = resData.substring(1, resData.length() - 1);
            //resData = "5";
            logger.info("main --> result:{}", resData);
            if (resData != null) {
                for (int i = 0; i < dataLists.size(); i++) {
                    logger.info("SendSms --> sendSms success jasonArray:{} ", dataList.get(i).toString());
                }
            }
        } catch (Exception e) {
            logger.error("SendSms --> sendSmsBatch ERROR:{}" + e.getMessage(), e);
            e.printStackTrace();
        }
        return resData;
    }

    public String sendSmsNew(String mobile, String tplId, Map<String, String> map) {
        String resData = null;
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost method = new HttpPost("http://sms.nuggets.dajie.com/sendSms?sendType=1");
        // 接收参数json列表
        try {
            JSONObject jsonParam = new JSONObject();
            JSONObject param = new JSONObject();
            JSONObject datas = new JSONObject();
            for (String key : map.keySet()) {
                param.put(key, map.get(key));
            }
            jsonParam.put("tpl_id", tplId);
            jsonParam.put("mobile", mobile);
            jsonParam.put("param", param);
            dataList.add(jsonParam);

            if (dataList.size() == 100) {
                datas.put("data", dataList);
                //    logger.info("SendSms -->dataSize:{} dataJsonStr:{}", dataList.size(), datas.toString());
                logger.info("SendSms -->dataSize:{}", dataList.size());
                StringEntity entity = new StringEntity(datas.toString(), "utf-8");//解决中文乱码问题
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                method.setEntity(entity);
                HttpResponse result = httpClient.execute(method);
                resData = EntityUtils.toString(result.getEntity());
                resData = resData.substring(1, resData.length() - 1);
                //resData = "1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0";
                // String[] res = resData.split(",");
                logger.info("SendSms --> result:{}", resData);
                dataList.clear();
            }
        } catch (Exception e) {
            logger.error("SendSms --> sendSmsNew ERROR:{}" + e.getMessage(), e);
            e.printStackTrace();
        }
        return "1";
    }

    public static String sendSmsSingle(String mobile, String tplId, Map<String, String> map) {
        String resData = null;
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost method = new HttpPost("http://sms.nuggets.dajie.com/sendSms");
        // 接收参数json列表
        try {
            JSONArray array = new JSONArray();
            JSONObject jsonParam = new JSONObject();
            JSONObject param = new JSONObject();
            JSONObject datas = new JSONObject();
            for (String key : map.keySet()) {
                param.put(key, map.get(key));
            }
            jsonParam.put("tpl_id", tplId);
            jsonParam.put("mobile", mobile);
            jsonParam.put("param", param);
            array.add(jsonParam);
            datas.put("data", array);
            StringEntity entity = new StringEntity(datas.toString(), "utf-8");//解决中文乱码问题
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            method.setEntity(entity);
            HttpResponse result = httpClient.execute(method);
            resData = EntityUtils.toString(result.getEntity());
            // resData = "1";
            logger.info("SendSms --> result:{}", resData);
        } catch (Exception e) {
            logger.error("SendSms --> sendSmsSingle ERROR:{}" + e.getMessage(), e);
            e.printStackTrace();
        }
        return "1";
    }

    public static void main(String[] args) {

    }
}
